package com.hc.android.research.dom;

import java.net.URI;
import java.net.URISyntaxException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Item {

	private int itemID;
	private String itemName;
	private URI itemURL;
	private List<ItemPrice> prices;

	public Item()
	{
		itemID = -1;
		itemName = "";
		itemURL = null;
		prices = new ArrayList<ItemPrice>();
	}
	
	public Item(int itemID, String itemName, String itemURL) throws URISyntaxException
	{
		this.itemID = itemID;
		this.itemName = itemName;
		if(itemURL.indexOf("http://") >= 0 && itemURL.indexOf("www.") < 0)
		{
			itemURL = itemURL.substring(7);
		}
		else if(itemURL.indexOf("http://") >= 0 && itemURL.indexOf("www.") >= 0)
		{
			itemURL = itemURL.substring(11);
		}
		if(itemURL.indexOf("www.") < 0)
		{
			itemURL = "www." + itemURL;
		}
		if(itemURL.indexOf("http://") < 0)
		{
			itemURL = "http://" + itemURL;
		}
		this.itemURL = new URI(itemURL);		
	}
	
	public void addPrice(String itemPrice, String priceDate)
	{
		ItemPrice ip = null;
		
		if(itemPrice != null)
		{
			ip = new ItemPrice();
			ip.setItemID(this.itemID);
			ip.setItemPrice(itemPrice);
		}
		
		Timestamp date = new Timestamp(0);
		if(priceDate != null)
		{
			try
			{
				Date parsedDate = ItemPrice.dateFormat.parse(priceDate);
				date = new Timestamp(parsedDate.getTime());				
			}
			catch(Exception e)
			{
				date = null;
			}
		}
		
		if(ip != null)
		{
			ip.setPriceDate(date);
		
			if(prices == null)
			{
				prices = new ArrayList<ItemPrice>();
			}
			prices.add(ip);
		}
	}

	public int getItemID() {
		return itemID;
	}

	public void setItemID(int itemID) {
		this.itemID = itemID;
	}

	public String getItemName() {
		return itemName;
	}

	public void setItemName(String itemName) {
		this.itemName = itemName;
	}

	public URI getItemURL() {
		return itemURL;
	}

	public void setItemURL(URI itemURL) {
		this.itemURL = itemURL;
	}

	public List<ItemPrice> getPrices() {
		return prices;
	}

	public void setPrices(List<ItemPrice> prices) {
		this.prices = prices;
	}
}
